package org.example.demo21.strategy.context;

import org.example.demo21.strategy.AgeStrategy;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 *
 * @author guocong
 * @since 2024/3/29 15:16
 */
@Component
public class AgeContext {

    /**
     * 策略集合
     * 灵魂所在，注入实例名及对应实例
     * 实例名：即component指定名称，默认为类名首字母小写名称
     */
    @Resource
    private Map<String, AgeStrategy> ageStrategyMap = new ConcurrentHashMap<>();

    /**
     * 初始化
     * @param ageStrategyMap 策略
     */
    public AgeContext(Map<String, AgeStrategy> ageStrategyMap) {
        ageStrategyMap.clear();
        this.ageStrategyMap.putAll(ageStrategyMap);
    }

    /**
     * 返回实际处理对象
     *
     * @param strategy 支付方式
     * @return 实际处理对象
     */
    public AgeStrategy getStrategy(String strategy) {
        return ageStrategyMap.get(strategy);
    }

}
